// common/nyz_area_picker/nyz_area_picker.js
const areaTool = require('../../utils/area.js');
var index = [0, 0, 0]
var provinces = areaTool.getProvinces();
var citys = areaTool.getCitys(index[0]);
var areas = areaTool.getAreas(index[0], index[1]);

Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		show: { //控制area_select显示隐藏
			type: Boolean,
			value: false
		},
		maskShow: { //是否显示蒙层
			type: Boolean,
			value: true
		},
		region: {
			type: Array,
			value: ''
		}
	},

	/**
	 * 组件的初始数据
	 */
	data: {

		province: '北京市',
		city: '北京市',
		area: '东城区'
	},
	lifetimes: {

		attached: function() {

			this.setData({
				provinces: provinces,
			})
		},
		ready: function() {
			this.setDefaultRegion();
		},
		detached: function() {

		}
	},

	pageLifetimes: {
		// 组件所在页面的生命周期函数
		show: function() {
			this.setDefaultRegion();
		},
		hide: function() {},
		resize: function() {},
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		handleNYZAreaChange: function(e) {
			var that = this;
			console.log("e:" + JSON.stringify(e));
			var value = e.detail.value;

			/**
			 * 滚动的是省
			 * 省改变 市、区都不变
			 */
			if (index[0] != value[0]) {
				index = [value[0], 0, 0]
				let selectCitys = areaTool.getCitys(index[0] - 1);
				let selectAreas = areaTool.getAreas(index[0] - 1, -1);

				that.setData({
					citys: selectCitys,
					areas: selectAreas,
					value: [index[0], 0, 0],
					province: provinces[index[0]],
					city: selectCitys[0],
					area: selectAreas[0]
				})



			} else if (index[1] != value[1]) {
				/**
				 * 市改变了 省不变 区变
				 */
				index = [value[0], value[1], 0]
				let selectCitys = areaTool.getCitys(index[0] - 1);
				let selectAreas = areaTool.getAreas(index[0] - 1, value[1] - 1);

				that.setData({
					//citys: selectCitys,
					areas: selectAreas,
					value: [index[0], index[1], 0],
					province: provinces[index[0]],
					city: selectCitys[index[1]],
					area: selectAreas[0]
				})



			} else if (index[2] != value[2]) {
				/**
				 * 区改变了
				 */
				index = [value[0], value[1], value[2]]
				let selectCitys = areaTool.getCitys(index[0] - 1);
				let selectAreas = areaTool.getAreas(index[0] - 1, value[1] - 1);

				that.setData({
					//citys: selectCitys,
					//areas: selectAreas,
					value: [index[0], index[1], index[2]],
					province: provinces[index[0]],
					city: selectCitys[index[1]],
					area: selectAreas[index[2]]
				})
				console.log("区改变了：" + this.data.province + this.data.city + this.data.area);

			}
		},
		/**
		 * 确定按钮的点击事件
		 */
		handleNYZAreaSelect: function(e) {
			//debugger;
			console.log("handleNYZAreaSelect e:" + JSON.stringify(e));
			var myEventDetail = e; // detail对象，提供给事件监听函数
			var myEventOption = {}; // 触发事件的选项
			this.triggerEvent('sureSelectArea', myEventDetail, myEventOption)
		},
		/**
		 * 取消按钮的点击事件
		 */
		handleNYZAreaCancle: function(e) {
			var that = this;
			console.log("e:" + JSON.stringify(e))
			this.setDefaultRegion();


			that.setData({
				show: false
			});
		},
		setDefaultRegion: function() {
			if (this.data.region && this.data.region.length === 3) {
				//debugger;
				let provinceindex = areaTool.getProvinceNameIndex(this.data.region[0]);
				let cityIndex = areaTool.getCityNameIndex(this.data.region[0], this.data.region[1]);
				let areaIndex = areaTool.getAreaNameIndex(this.data.region[0], this.data.region[1], this.data.region[2]);
				this.setData({
					citys: areaTool.getCitys(provinceindex),
					areas: areaTool.getAreas(provinceindex, cityIndex),
					province: this.data.region[0],
					city: this.data.region[1],
					area: this.data.region[2],
				})
				index = [provinceindex + 1, cityIndex + 1, areaIndex + 1];
				this.setData({
					value: [provinceindex + 1, cityIndex + 1, areaIndex + 1],

				})
			} else {
				//debugger;
				this.setData({
					citys: areaTool.getCitys(index[0]),
					areas: areaTool.getAreas(index[0], index[1]),
					value: [0, 0, 0],
				})
			}
		}
	}
})
